package com.ylxl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ylxl.common.exception.BaseException;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.Container;
import com.ylxl.entity.Device;
import com.ylxl.entity.Label;
import com.ylxl.entity.reponse.ContainerVO;
import com.ylxl.entity.request.ContainerCmdOrMdy;
import com.ylxl.entity.request.QueryContainer;
import com.ylxl.entity.request.QueryDevice;
import com.ylxl.entity.request.QueryLabel;
import com.ylxl.mapper.ContainerMapper;
import com.ylxl.mapper.DeviceMapper;
import com.ylxl.service.IContainerService;
import com.ylxl.service.ILabelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther Gilbert
 * @Date 2023/6/26 13:52
 */
@Service
public class ContainerServiceImpl extends ServiceImpl<ContainerMapper, Container> implements IContainerService {

    @Resource
    private ContainerMapper containerMapper;
    @Resource
    private ILabelService labelService;
    @Resource
    private DeviceMapper deviceMapper;

    @Override
    public List<ContainerVO> selectContainerList(QueryContainer queryContainer) {
        LambdaQueryWrapper<Container> queryContainerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        queryContainerLambdaQueryWrapper.eq(StringUtils.isNotEmpty(queryContainer.getContainerNumber()),Container::getContainerNumber,queryContainer.getContainerNumber())
                .eq(StringUtils.isNotEmpty(queryContainer.getStatus()),Container::getStatus,queryContainer.getStatus())
                .eq(StringUtils.isNotEmpty(queryContainer.getType()),Container::getType,queryContainer.getType());
        List<Container> containers = containerMapper.selectList(queryContainerLambdaQueryWrapper);
        //查询出所有标签信息
        List<Label> labels = labelService.selectLabelList(new QueryLabel());
        Map<Integer, String> collect = labels.stream().collect(Collectors.toMap(Label::getId, Label::getLabelName, (k1, k2) -> k1));
        return containers.stream().map(res->{
            ContainerVO containerVO = new ContainerVO();
            BeanUtil.copyProperties(res, containerVO);//属性拷贝
            //label_id是字符串拼接
            List<String> list = Arrays.asList(res.getLabelId().split(","));
            List<String> LabelNameList = Lists.newLinkedList();
            list.forEach(l->{
                String LabelName = collect.get(Integer.valueOf(l));
                if(StringUtils.isNotEmpty(LabelName)){
                    LabelNameList.add(LabelName);
                }
            });
            containerVO.setLabelName(String.join(",",LabelNameList));
            return containerVO;
        }).collect(Collectors.toList());
    }

    @Override
    public int insertContainer(ContainerCmdOrMdy containerCmdOrMdy) {
        Container container = new Container();
        BeanUtil.copyProperties(containerCmdOrMdy, container);//属性拷贝
        return containerMapper.insert(container);
    }

    @Override
    public int updateContainer(ContainerCmdOrMdy containerCmdOrMdy) {
        if(StringUtils.isEmpty(containerCmdOrMdy.getId())){
            throw new BaseException("编辑时集装箱id不能为空");
        }
        Container container = new Container();
        BeanUtil.copyProperties(containerCmdOrMdy, container);//属性拷贝

        return containerMapper.updateById(container);
    }

    @Override
    public ContainerVO selctById(Integer id) {
        Container container = containerMapper.selectById(id);
        //查询出所有标签信息
        List<Label> labels = labelService.selectLabelList(new QueryLabel());
        Map<Integer, String> collect = labels.stream().collect(Collectors.toMap(Label::getId, Label::getLabelName, (k1, k2) -> k1));
        ContainerVO containerVO = new ContainerVO();
        BeanUtil.copyProperties(container, containerVO);//属性拷贝
        //label_id是字符串拼接
        List<String> list = Arrays.asList(container.getLabelId().split(","));
        List<String> LabelNameList = Lists.newLinkedList();
        list.forEach(l->{
            String LabelName = collect.get(Integer.valueOf(l));
            if(StringUtils.isNotEmpty(LabelName)){
                LabelNameList.add(LabelName);
            }
        });
        containerVO.setLabelName(String.join(",",LabelNameList));

        return containerVO;
    }

    @Override
    public int deleteById(Integer id) {
        return containerMapper.deleteById(id);
    }

    @Override
    public List<Device> selectDeviceList(QueryDevice queryDevice) {
        LambdaQueryWrapper<Device> deviceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceLambdaQueryWrapper.like(StringUtils.isNotEmpty(queryDevice.getDeviceName()),Device::getDeviceName,queryDevice.getDeviceName());
        return deviceMapper.selectList(deviceLambdaQueryWrapper);
    }

}
